// 确保API_BASE_URL指向正确的后端服务地址
const API_BASE_URL = 'http://localhost:8080/hrm/api';

// 认证相关API
const AuthAPI = {
  login: (credentials) => apiRequest(`${API_BASE_URL}/auth/login`, 'POST', credentials),
  logout: () => apiRequest(`${API_BASE_URL}/auth/logout`, 'POST'),
  getUserInfo: () => apiRequest(`${API_BASE_URL}/auth/user-info`, 'GET'),
  // 修复注册API的URL
  register: async (data) => {
      const { username, password } = data;
      return apiRequest(`${API_BASE_URL}/auth/register`, 'POST', { username, password });
  },
};

// 仪表盘相关API
const DashboardAPI = {
  getMetrics: () => apiRequest(`${API_BASE_URL}/dashboard/metrics`, 'GET')
};

// 项目相关API
const ProjectAPI = {
  getAllProjects: () => apiRequest(`${API_BASE_URL}/projects`, 'GET'),
  getProjectById: (id) => apiRequest(`${API_BASE_URL}/projects/${id}`, 'GET'),
  createProject: (projectData) => apiRequest(`${API_BASE_URL}/projects`, 'POST', projectData),
  updateProject: (id, projectData) => apiRequest(`${API_BASE_URL}/projects/${id}`, 'PUT', projectData),
  deleteProject: (id) => apiRequest(`${API_BASE_URL}/projects/${id}`, 'DELETE')
};

// 任务相关API
const TaskAPI = {
  getAllTasks: () => apiRequest(`${API_BASE_URL}/tasks`, 'GET'),
  getTasksByProjectId: (projectId) => apiRequest(`${API_BASE_URL}/projects/${projectId}/tasks`, 'GET'),
  getTaskById: (id) => apiRequest(`${API_BASE_URL}/tasks/${id}`, 'GET'),
  createTask: (taskData) => apiRequest(`${API_BASE_URL}/tasks`, 'POST', taskData),
  updateTask: (id, taskData) => apiRequest(`${API_BASE_URL}/tasks/${id}`, 'PUT', taskData),
  deleteTask: (id) => apiRequest(`${API_BASE_URL}/tasks/${id}`, 'DELETE'),
  updateTaskStatus: (id, status) => apiRequest(`${API_BASE_URL}/tasks/${id}/status`, 'PATCH', { status })
};

// 人才相关API
const TalentAPI = {
  getAllTalents: () => apiRequest(`${API_BASE_URL}/talents`, 'GET'),
  getTalentById: (id) => apiRequest(`${API_BASE_URL}/talents/${id}`, 'GET'),
  createTalent: (talentData) => apiRequest(`${API_BASE_URL}/talents`, 'POST', talentData),
  updateTalent: (id, talentData) => apiRequest(`${API_BASE_URL}/talents/${id}`, 'PUT', talentData),
  deleteTalent: (id) => apiRequest(`${API_BASE_URL}/talents/${id}`, 'DELETE'),
  getTalentAvailability: (id) => apiRequest(`${API_BASE_URL}/talents/${id}/availability`, 'GET')
};

// 报表相关API
const ReportAPI = {
  getProjectCompletion: () => apiRequest(`${API_BASE_URL}/reports/project-completion`, 'GET'),
  getResourceUtilization: () => apiRequest(`${API_BASE_URL}/reports/resource-utilization`, 'GET'),
  getSkillDistribution: () => apiRequest(`${API_BASE_URL}/reports/skill-distribution`, 'GET'),
  getTaskCompletionTrend: (period) => apiRequest(`${API_BASE_URL}/reports/task-completion-trend?period=${period}`, 'GET')
};

// 通知相关API
const NotificationAPI = {
  getNotifications: () => apiRequest(`${API_BASE_URL}/notifications`, 'GET'),
  markAsRead: (id) => apiRequest(`${API_BASE_URL}/notifications/${id}/read`, 'PATCH'),
  markAllAsRead: () => apiRequest(`${API_BASE_URL}/notifications/read-all`, 'PATCH')
};

// 技能管理相关API
const SkillAPI = {
  getAllSkills: () => apiRequest(`${API_BASE_URL}/skills`, 'GET'),
  getSkillById: (id) => apiRequest(`${API_BASE_URL}/skills/${id}`, 'GET'),
  createSkill: (skillData) => apiRequest(`${API_BASE_URL}/skills`, 'POST', skillData),
  updateSkill: (id, skillData) => apiRequest(`${API_BASE_URL}/skills/${id}`, 'PUT', skillData),
  deleteSkill: (id) => apiRequest(`${API_BASE_URL}/skills/${id}`, 'DELETE'),
  getSkillCategories: () => apiRequest(`${API_BASE_URL}/skills/categories`, 'GET'),
  getSkillByCategory: (category) => apiRequest(`${API_BASE_URL}/skills/category/${category}`, 'GET')
};

// 个人资料API
const ProfileAPI = {
  getProfile: () => apiRequest(`${API_BASE_URL}/profile`, 'GET'),
  updateProfile: (profileData) => apiRequest(`${API_BASE_URL}/profile`, 'PUT', profileData),
  updateAvatar: (avatarFile) => {
    const formData = new FormData();
    formData.append('avatar', avatarFile);
    return apiRequest(`${API_BASE_URL}/profile/avatar`, 'POST', formData, {
      headers: {'Content-Type': 'multipart/form-data'}
    });
  }
};

// 请假API
const LeaveAPI = {
  getLeaveHistory: () => apiRequest(`${API_BASE_URL}/leave`, 'GET'),
  submitLeaveRequest: (leaveData) => apiRequest(`${API_BASE_URL}/leave`, 'POST', leaveData),
  getLeaveDetail: (leaveId) => apiRequest(`${API_BASE_URL}/leave/${leaveId}`, 'GET'),
  cancelLeaveRequest: (leaveId) => apiRequest(`${API_BASE_URL}/leave/${leaveId}`, 'DELETE')
};

// 薪资API
const SalaryAPI = {
  getSalaryHistory: () => apiRequest(`${API_BASE_URL}/salary`, 'GET'),
  getSalaryDetail: (salaryId) => apiRequest(`${API_BASE_URL}/salary/${salaryId}`, 'GET'),
  getSalaryTrend: (year) => apiRequest(`${API_BASE_URL}/salary/trend?year=${year || new Date().getFullYear()}`, 'GET')
};

// 系统设置API
const SystemAPI = {
  // 用户管理
  getUsers: () => apiRequest(`${API_BASE_URL}/system/users`, 'GET'),
  createUser: (userData) => apiRequest(`${API_BASE_URL}/system/users`, 'POST', userData),
  updateUser: (userId, userData) => apiRequest(`${API_BASE_URL}/system/users/${userId}`, 'PUT', userData),
  deleteUser: (userId) => apiRequest(`${API_BASE_URL}/system/users/${userId}`, 'DELETE'),
  // 部门管理
  getDepartments: () => apiRequest(`${API_BASE_URL}/system/departments`, 'GET'),
  createDepartment: (deptData) => apiRequest(`${API_BASE_URL}/system/departments`, 'POST', deptData),
  updateDepartment: (deptId, deptData) => apiRequest(`${API_BASE_URL}/system/departments/${deptId}`, 'PUT', deptData),
  deleteDepartment: (deptId) => apiRequest(`${API_BASE_URL}/system/departments/${deptId}`, 'DELETE'),
  // 系统配置
  getSystemConfig: () => apiRequest(`${API_BASE_URL}/system/config`, 'GET'),
  updateSystemConfig: (configData) => apiRequest(`${API_BASE_URL}/system/config`, 'PUT', configData)
};

// 保存系统配置 - 使用与updateSystemConfig相同的实现
function saveSystemConfig(configData) {
  return SystemAPI.updateSystemConfig(configData);
}

// 将方法添加到全局api对象
const api = {
  // 认证相关
  login: AuthAPI.login,
  logout: AuthAPI.logout,
  getUserInfo: AuthAPI.getUserInfo,
  register: AuthAPI.register,  // 添加注册方法
  // 仪表盘相关
  getMetrics: DashboardAPI.getMetrics,
  // 项目相关
  getAllProjects: ProjectAPI.getAllProjects,
  getProjectById: ProjectAPI.getProjectById,
  createProject: ProjectAPI.createProject,
  updateProject: ProjectAPI.updateProject,
  deleteProject: ProjectAPI.deleteProject,
  // 任务相关
  getAllTasks: TaskAPI.getAllTasks,
  getTasksByProjectId: TaskAPI.getTasksByProjectId,
  getTaskById: TaskAPI.getTaskById,
  createTask: TaskAPI.createTask,
  updateTask: TaskAPI.updateTask,
  deleteTask: TaskAPI.deleteTask,
  updateTaskStatus: TaskAPI.updateTaskStatus,
  // 人才相关
  getAllTalents: TalentAPI.getAllTalents,
  getTalentById: TalentAPI.getTalentById,
  createTalent: TalentAPI.createTalent,
  updateTalent: TalentAPI.updateTalent,
  deleteTalent: TalentAPI.deleteTalent,
  getTalentAvailability: TalentAPI.getTalentAvailability,
  // 报表相关
  getProjectCompletion: ReportAPI.getProjectCompletion,
  getResourceUtilization: ReportAPI.getResourceUtilization,
  getSkillDistribution: ReportAPI.getSkillDistribution,
  getTaskCompletionTrend: ReportAPI.getTaskCompletionTrend,
  // 通知相关
  getNotifications: NotificationAPI.getNotifications,
  markAsRead: NotificationAPI.markAsRead,
  markAllAsRead: NotificationAPI.markAllAsRead,
  // 技能相关
  getAllSkills: SkillAPI.getAllSkills,
  getSkillById: SkillAPI.getSkillById,
  createSkill: SkillAPI.createSkill,
  updateSkill: SkillAPI.updateSkill,
  deleteSkill: SkillAPI.deleteSkill,
  getSkillCategories: SkillAPI.getSkillCategories,
  getSkillByCategory: SkillAPI.getSkillByCategory,
  // 个人资料相关
  getProfile: ProfileAPI.getProfile,
  updateProfile: ProfileAPI.updateProfile,
  updateAvatar: ProfileAPI.updateAvatar,
  // 请假相关
  getLeaveHistory: LeaveAPI.getLeaveHistory,
  submitLeaveRequest: LeaveAPI.submitLeaveRequest,
  getLeaveDetail: LeaveAPI.getLeaveDetail,
  cancelLeaveRequest: LeaveAPI.cancelLeaveRequest,
  // 薪资相关
  getSalaryHistory: SalaryAPI.getSalaryHistory,
  getSalaryDetail: SalaryAPI.getSalaryDetail,
  getSalaryTrend: SalaryAPI.getSalaryTrend,
  // 系统设置相关
  getUsers: SystemAPI.getUsers,
  createUser: SystemAPI.createUser,
  updateUser: SystemAPI.updateUser,
  deleteUser: SystemAPI.deleteUser,
  getDepartments: SystemAPI.getDepartments,
  createDepartment: SystemAPI.createDepartment,
  updateDepartment: SystemAPI.updateDepartment,
  deleteDepartment: SystemAPI.deleteDepartment,
  getSystemConfig: SystemAPI.getSystemConfig,
  saveSystemConfig: saveSystemConfig,
  updateSystemConfig: SystemAPI.updateSystemConfig
};